package org.example.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.domain.ResponseResult;
import org.example.domain.dto.insertUserDto;
import org.example.domain.entity.UserRole;
import org.example.mapper.UserRoleMapper;
import org.example.service.UserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    private final UserRoleMapper userRoleMapper;


    @Override
    public ResponseResult getRoleIdByUserId(Integer id) {
        List<Integer> ids = userRoleMapper.getRoleIdByUserId(id);
        return ResponseResult.okResult(ids);
    }

    @Override
    public void deleteByUserId(Integer id) {
        userRoleMapper.deleteByUserId(id);
    }

    @Override
    public void addUserRole(insertUserDto insertUserDto) {
        Integer userId = insertUserDto.getId();
        Integer[] roleIds = insertUserDto.getRolesId();
        List<UserRole> userRoleList = new ArrayList<>();
        for(int i=0;i<roleIds.length;i++){
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleIds[i]);
            userRole.setUserId(userId);
            userRoleList.add(userRole);
        }
        saveBatch(userRoleList);
    }
}
